//
//  YTTabBarViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/4/27.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTTabBarViewController.h"
#import "YTNavigationController.h"

#import "YTOrderListViewController.h"
#import "YTOrderManagerViewController.h"
#import "YTChatListViewController.h"
#import "YTProfileViewController.h"
#import "YTInOrderViewController.h"
#import "YTLoginViewController.h"

@interface YTTabBarViewController ()

@end

@implementation YTTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //item
    YTNavigationController *orderList = [self configStoryboardWithName:@"YTOrderListStoryboard" withController:@"YTNavigationController" identifier:@"YTOrderListStoryboard"];
    orderList.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"接单" image:[UIImage imageNamed:@"tabbar_orderlist"] tag:0];
    
    YTNavigationController *myOrder = [self configStoryboardWithName:@"YTOrderManagerStoryboard" withController:@"YTNavigationController" identifier:@"YTOrderManagerStoryboard"];
    myOrder.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"行程" image:[UIImage imageNamed:@"tabbar_myorder"] tag:1];
    
    YTInOrderViewController *inOrder = [[YTInOrderViewController alloc] init];
    inOrder.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"代客下单" image:[UIImage imageNamed:@"tabbar_delegate"] tag:2];
    
    YTNavigationController *chatList = [self configStoryboardWithName:@"YTChatListStoryboard" withController:@"YTNavigationController" identifier:@"YTChatListStoryboard"];
    chatList.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"消息" image:[UIImage imageNamed:@"tabbar_chat"] tag:3];
    
    YTNavigationController *profile = [self configStoryboardWithName:@"YTProfileStoryboard" withController:@"YTNavigationController" identifier:@"YTProfileStoryboard"];
    profile.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"tabbar_profile"] tag:4];
    
    self.viewControllers = @[orderList, myOrder, inOrder, chatList, profile];
    
    // 目前不知道是不是实时获取的
    int count =  [[RCIMClient sharedRCIMClient] getTotalUnreadCount];
    if (count > 0) {
        chatList.tabBarItem.badgeValue = [NSString stringWithFormat:@"%zd", count];
    }
    
    @weakify(self);
    [[[[NSNotificationCenter defaultCenter]
       rac_addObserverForName:YTPickOrderSuccessNofitication object:nil]
       subscribeOn:[RACScheduler mainThreadScheduler]]
       subscribeNext:^(NSNotification *notify) { // 这里有回来订单id
           @strongify(self);
           self.selectedIndex = 1;
       }];
}

- (YTNavigationController *)configStoryboardWithName:(NSString *)storyName
                                      withController:(NSString *)vc
                                          identifier:(NSString *)identifier {
    UIStoryboard *story = [UIStoryboard storyboardWithName:storyName bundle:nil];
    YTNavigationController *viewController = (YTNavigationController *)NSClassFromString(vc);
    viewController = [story instantiateViewControllerWithIdentifier:identifier];
    return viewController;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
